package com.group.mall.service.Impl;

import com.group.mall.common.Result;
import com.group.mall.domain.POJO.ShoppingCart;
import com.group.mall.mapper.CartMapper;
import com.group.mall.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @Author：xiexu
 * @Date：2024/6/18 14:21
 */
@Slf4j
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    CartMapper cartMapper;

    @Override
    public Result addCart(ShoppingCart cart) {
//        Cart oldCart = cartMapper.selectCartByUserIdAndItemId(cart.getGoodsId(), cart.getUserId());
        ShoppingCart oldCart = cartMapper.selectCartByUserIdAndItemId(cart.getGoodsProId());
        Result result_back = new Result();
        System.out.println("111"+ cart);
        if (oldCart == null) {
            boolean result = cartMapper.addCart(cart);
            if (result) {
                result_back.setMsg("新增成功");
            } else {
                result_back.setMsg("新增失败！");
            }
        }
        else {
            if (oldCart.getVersion().equals(cart.getVersion())) {
                if (updateCart(cart)) {
                    result_back.setMsg("更新购物车成功");
                } else {
                    result_back.setMsg("更新购物车失败！");
                }
            } else if (oldCart.getColor().equals(cart.getColor())) {
                if (updateCart(cart)) {
                    result_back.setMsg("更新购物车成功");
                } else {
                    result_back.setMsg("更新购物车失败！");
                }
            } else {
                result_back.setMsg("购物车已存在！");
            }

        }
        return result_back;
    }

    @Override
    public boolean updateCart(ShoppingCart cart) {
//        Cart oldCart = cartMapper.selectCartByUserIdAndItemId(cart.getGoodsId(), cart.getUserId());
        ShoppingCart oldCart = cartMapper.selectCartByUserIdAndItemId(cart.getGoodsProId());
        int newBuyCount = oldCart.getCount() + cart.getCount();
        Double newPrice = oldCart.getPrice() + cart.getPrice();
        cart.setCount(newBuyCount);
        //cart.setCartId(oldCart.getCartId());
        cart.setPrice(oldCart.getPrice());
        cart.setPrice(newPrice);
        cart.setVersion(cart.getVersion());
        cart.setColor(cart.getColor());
        cart.setUpdateTime(LocalDate.now());
        boolean result = cartMapper.updateCart(cart);
        if (result) {
            return true;
        }
        else {
            return false;
        }

    }

    @Override
    public ShoppingCart selectCartByUserIdAndItemId(ShoppingCart cart) {
        Integer goodsId = cart.getGoodsProId();
        Integer userId = cart.getUserId();
//        return cartMapper.selectCartByUserIdAndItemId(goodsId, userId);
        return cartMapper.selectCartByUserIdAndItemId(goodsId);
    }


    @Override
    public List<ShoppingCart> listCart(Integer userId) {
        return cartMapper.listCart(userId);
    }

    @Override
    public List<ShoppingCart> getCartById(Integer userId) {
        return cartMapper.getCartById(userId);
    }

    @Override
    public boolean deleteCartById(Integer cartId, Integer userId) {
        return cartMapper.deleteCartById(cartId, userId);
    }
}
